<template>
  <div class="settings-container">
    <el-card
      >=={{ activeName }}
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="角色管理" name="first">
          <div>
            <el-button type="primary" @click="addRoles">添加角色</el-button>
            <!-- 表格 -->
            <el-table
              :default-sort="{ prop: 'name', order: 'descending' }"
              :data="tableData"
              border
              style="width: 100%; margin: 20px 0"
            >
              <el-table-column fixed type="index" label="序号" width="150">
              </el-table-column>
              <el-table-column sortable prop="name" label="角色名">
              </el-table-column>
              <el-table-column sortable prop="description" label="描述">
              </el-table-column>
              <el-table-column fixed="right" label="操作">
                <template slot-scope="scope">
                  <el-button
                    @click="handleClick(scope.row.id)"
                    type="text"
                    size="small"
                    >分配权限</el-button
                  >
                  <el-button
                    type="text"
                    size="small"
                    @click="handleEdit(scope.row)"
                    >修改</el-button
                  >
                  <el-button type="text" size="small" @click="del(scope.row.id)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>

            <!-- 分页 -->
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page.sync="paramsObj.page"
              :page-size="paramsObj.pagesize"
              :page-sizes="[1, 3, 5, 7, 10]"
              layout="slot, sizes, prev, pager, next, jumper"
              :total="total"
              >共{{ total }}条
            </el-pagination>
          </div>
        </el-tab-pane>
        <el-tab-pane label="公司信息" name="second">
          <el-form ref="form" :model="companyForm" label-width="80px">
            <el-form-item label="企业名称">
              <el-input disabled v-model="companyForm.name"></el-input>
            </el-form-item>
            <el-form-item label="公司地址">
              <el-input
                disabled
                v-model="companyForm.companyAddress"
              ></el-input>
            </el-form-item>
            <el-form-item label="公司电话">
              <el-input disabled v-model="companyForm.companyPhone"></el-input>
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input disabled v-model="companyForm.mailbox"></el-input>
            </el-form-item>
            <el-form-item label="备注">
              <el-input disabled v-model="companyForm.remarks"></el-input>
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </el-card>

    <!-- 对话框 -->
    <roleDia
      :row="row"
      :is-edit="isEdit"
      @success="success"
      :dialogFormVisible.sync="dialogFormVisible"
    ></roleDia>

    <!-- 分配权限对话框 -->
    <!-- 分配权限的弹层 -->
    <el-dialog
      title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
      :visible.sync="showDialogAssign"
    >
      <assign-permission
        v-if="showDialogAssign"
        :roleId="roleId"
        @close="close"
      />
    </el-dialog>
  </div>
</template>

<script>
import {
  getCompanyInfoByIdApi,
  getRolesListApi,
  delRolesApi
} from '@/api/settings'
import roleDia from './components/roleDia.vue'
import assignPermission from './assignPermission'
export default {
  components: {
    roleDia,
    assignPermission
  },
  data() {
    return {
      activeName: 'first',
      companyForm: {
        name: '',
        companyAddress: '',
        companyPhone: '',
        mailbox: '',
        remarks: ''
      },
      tableData: [],
      paramsObj: {
        page: 1,
        pagesize: 5
      },
      dialogFormVisible: false,
      row: {},
      isEdit: false,
      total: 0,
      showDialogAssign: false, //  分配权限的对话框bool值
      roleId: ''
    }
  },
  created() {
    /* 获取角色列表 */
    this.getRoles()
    /* 获取公司信息 */
    this.getCompanyInfo()
  },
  methods: {
    close() {
      this.showDialogAssign = false
    },
    handleClick(id) {
      /* 分配权限按钮
      1、显示对话框
      2、获取所有的权限===树形展示
      3、获取当前角色所拥有的权限===并展示到树形结构中以选中的形式来展示
      4、点击对话框确认按钮
        4.1 封装api接口函数
        4.2 调用接口函数发送请求  分配权限的请求
       */
      console.log(id)
      this.showDialogAssign = true
      /* 获取角色id */
      this.roleId = id
    },

    /* 过去公司信息 */
    async getCompanyInfo() {
      const res = await getCompanyInfoByIdApi(
        this.$store.state.user.userInfo.companyId
      )
      console.log(res, 66)
      this.companyForm = res.data
    },
    /* 获取角色列表 */
    async getRoles() {
      const res = await getRolesListApi(this.paramsObj)
      this.tableData = res.data.rows
      //  获取总条数
      this.total = res.data.total
    },

    /* 添加角色 */
    addRoles() {
      /*
      1、显示对话框
      2、校验
      3、点击确认按钮
        3.1 封装api接口函数
        3.2 调用请求函数
        3.3 刷新页面+ 关闭对话框
       */
      this.dialogFormVisible = true
      this.isEdit = false
      this.row = {
        name: '',
        description: ''
      }
    },

    /* 添加角色成功后 */
    success() {
      /* 获取角色列表  刷新页面 */
      this.getRoles()
    },
    /* 编辑角色 */
    handleEdit(row) {
      console.log(row, 5)
      /*
      1、显示对话框
      2、获取当前点击行的数据并展示到对话框表单中  ======共用对话框
      3、点击确认按钮
        3.1 封装api  编辑的接口函数
        3.2 判断点击确认按钮是要进行添加还是编辑
        3.3 清空数据关闭对话框+刷新页面
       */
      this.dialogFormVisible = true
      this.row = row
      this.isEdit = true
    },
    del(id) {
      console.log(id, '2222')
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          //  确认按钮
          await delRolesApi(id)

          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          /* 获取角色列表===刷新页面 */
          this.getRoles()
        })
        .catch(() => {
          //  取消按钮
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },

    /* 分页 */
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.paramsObj.pagesize = val
      /* 获取角色列表 */
      this.getRoles()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.paramsObj.page = val
      /* 获取角色列表 */
      this.getRoles()
    }
  }
}
</script>
<style lang='scss'  scoped>
.settings-container {
  .el-form {
    width: 500px;
    margin-left: 100px;
  }
  .el-pagination {
    text-align: right;
  }

  .el-pagination {
    line-height: 28px;
  }
}
</style>
